﻿using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Chromium_Updater
{
    public partial class SettingsForm : Form
    {
        static bool open = false;
        //
        clsRegistry reg;

        public SettingsForm()
        {
            InitializeComponent();
        }

        public bool _Show()
        {
            if (!open)
            {
                this.Show();
                return true;
            }
            return false;
        }

        private void SettingsForm_Load(object sender, System.EventArgs e)
        {
            open = true;
            //
            comboBox1.SelectedIndex = Settings.DownloadAs;
            checkBox1.Checked       = Settings.Install;
            checkBox2.Checked       = Settings.DeleteAfter;
            textBox1.Text           = Settings.DownloadTo;
            checkBox3.Checked       = Settings.AppendFileName;
            checkBox4.Checked       = Settings.MinimizeOnStart;
            checkBox5.Checked       = Settings.HideDownloader;
            checkBox7.Checked       = Settings.OpenChromiumAfter;
            //
            reg = new clsRegistry();
            if (reg.GetStringValue(Registry.CurrentUser, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "Chromium Updater") != null)
                checkBox6.Checked = true;
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (comboBox1.SelectedIndex != Settings.DownloadAs ||
                checkBox1.Checked       != Settings.Install ||
                textBox1.Text           != Settings.DownloadTo ||
                checkBox2.Checked       != Settings.DeleteAfter ||
                checkBox3.Checked       != Settings.AppendFileName ||
                checkBox4.Checked       != Settings.MinimizeOnStart ||
                checkBox5.Checked       != Settings.HideDownloader ||
                checkBox7.Checked       != Settings.OpenChromiumAfter)
            {
                if (MessageBox.Show("Would you like to save your changes before closing?", "Chromium Updater - Updater", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Save();
                    return;
                }
            }
            //
            open = false;
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            FolderBrowserDialog f = new FolderBrowserDialog();
            if (f.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = f.SelectedPath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void Save()
        {
            Settings.DownloadAs        = comboBox1.SelectedIndex;
            Settings.Install           = checkBox1.Checked;
            Settings.DeleteAfter       = checkBox2.Checked;
            Settings.DownloadTo        = textBox1.Text;
            Settings.AppendFileName    = checkBox3.Checked;
            Settings.MinimizeOnStart   = checkBox4.Checked;
            Settings.HideDownloader    = checkBox5.Checked;
            Settings.OpenChromiumAfter = checkBox7.Checked;
            //
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 1)
            {
                checkBox1.Enabled = false;
                checkBox2.Enabled = false;
                checkBox7.Enabled = false;
            }
            else
            {
                checkBox1.Enabled = true;
                checkBox2.Enabled = true;
                checkBox7.Enabled = true;
            }
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked)
                reg.SetStringValue(Registry.CurrentUser, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "Chromium Updater", Application.ExecutablePath.ToString());
            else
                reg.DeleteValue(Registry.CurrentUser, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "Chromium Updater");
        }
    }
}
